#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/7/28 11:37
# @Author  : Samge
import random

from itkz.resources.utils import log
from itkz.spider_ext.datas import data_agent
from itkz.spider_ext.middlewares.m_base import BaseMiddleware


class RandomUserAgentMiddleware(BaseMiddleware):

    def process_request(self, request, spider):

        if not self.is_use_agent:
            return

        user_agent = request.headers.get("User-Agent")
        if user_agent and 'Scrapy' not in str(user_agent):
            log.log_info(spider, f'请求对象中已经有用户代理字段，{user_agent} {request.url}')
            return

        request.headers["User-Agent"] = random.choice(data_agent.user_agent_list)
        log.log_info(spider, '获取到的用户代理user-agent： {}  url={}'.format(request.headers['User-Agent'], request.url))
